package com.fili.gui.sources;

import com.fili.core.sources.Sources;
import com.fili.core.sources.SourceMapper;
import com.fili.core.sources.MappedSource;
import com.fili.core.utils.ImageLoader;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 16, 2006
 * Time: 6:58:26 PM
 * Revision: 0
 */
public class SourceTree extends JComponent implements MouseListener
{
    public static final String EVENT_OPEN = "SOURCE_TREE_EVENT_OPEN";
    private Sources sources;
    private JTree tree;
    private DefaultTreeModel treeModel;
    private DefaultMutableTreeNode treeRoot;

    private static ImageIcon ico_j_folder_open = null;
    private static ImageIcon ico_j_folder_closed = null;
    private static ImageIcon ico_j_file = null;
    private static ImageIcon ico_h_file = null;
    private static ImageIcon ico_unk_file = null;


    public SourceTree(Sources sources)
    {
        this.sources = sources;
        setMinimumSize(new Dimension(120, 120));
        treeRoot = new DefaultMutableTreeNode("Project");
        treeModel = new DefaultTreeModel(treeRoot);
        for (int i = 0; i < sources.getBigFiles().length; i++)
        {
            SourceMapper mapper = sources.getBigFiles()[i];
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(mapper);
            treeModel.insertNodeInto(node, treeRoot, treeRoot.getChildCount());
            for (int j = 0; j < mapper.getMappedSources().length; j++)
            {
                DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(mapper.getMappedSources()[j]);
                treeModel.insertNodeInto(node2, node, node.getChildCount());
            }
        }
        tree = new JTree(treeModel);
        tree.addMouseListener(this);

        setLayout(new BorderLayout());
        add(new JScrollPane(tree), BorderLayout.CENTER);

        if (ico_j_folder_open == null)
            ico_j_folder_open = ImageLoader.loadImage("res/img/j_folder_open.png");
        if (ico_j_folder_closed == null)
            ico_j_folder_closed = ImageLoader.loadImage("res/img/j_folder_closed.png");
        if (ico_j_file == null)
            ico_j_file = ImageLoader.loadImage("res/img/j_file.png");
        if (ico_h_file == null)
            ico_h_file = ImageLoader.loadImage("res/img/h_file.png");
        if (ico_unk_file == null)
            ico_unk_file = ImageLoader.loadImage("res/img/unk_file.png");

        tree.setCellRenderer(new SourceCellRenderer());
    }

    public void mouseClicked(MouseEvent e)
    {
        if (e.getClickCount() > 1)
        {
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            if (path == null)
                return;
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            if (node == null)
                return;
            if (node.getUserObject() instanceof MappedSource)
                firePropertyChange(EVENT_OPEN, null, node.getUserObject());
        }
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    private class SourceCellRenderer extends DefaultTreeCellRenderer
    {
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
        {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode)
            {
                Object obj = ((DefaultMutableTreeNode) value).getUserObject();
                if (obj instanceof MappedSource)
                {
                    //it's a smallFile
                    String s = ((MappedSource) obj).getSourcePath().toUpperCase();
                    if (s.endsWith(".H"))
                        setIcon(ico_h_file);
                    else
                        if (s.endsWith(".JAVA"))
                            setIcon(ico_j_file);
                        else
                            setIcon(ico_unk_file);
                }
                else
                    if (obj instanceof SourceMapper)
                    {
                        //it's a bigfile
                        if (expanded)
                            setIcon(ico_j_folder_open);
                        else
                            setIcon(ico_j_folder_closed);
                    }
                    else
                        if (obj instanceof String)
                        {
                            //it's root
                            setIcon(null);
                        }
            }
            return this;
        }
    }
}
